<template>
  <div>
    <el-table :data="orderRecords" style="width: 100%">
      <el-table-column prop="id" label="订单号" width="180"></el-table-column>
      <el-table-column label="课程" min-width="250">
        <template #default="scope">
          <div class="order-course">
            <img :src="scope.row.course.image" class="course-image">
            <span class="course-name">{{ scope.row.course.name }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="amount" label="价格" width="100">
        <template #default="scope">
          ¥{{ scope.row.amount }}
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="100">
        <template #default="scope">
          <el-tag :type="getOrderStatusType(scope.row.status)">{{ getOrderStatusText(scope.row.status) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180"></el-table-column>
      <el-table-column label="操作" width="150">
        <template #default="scope">
          <el-button type="primary" size="small" @click="viewOrder(scope.row)">查看</el-button>
          <el-button 
            v-if="scope.row.status === 'pending'" 
            type="success" 
            size="small" 
            @click="payOrder(scope.row)"
          >支付</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <div class="empty-data" v-if="orderRecords.length === 0">
      <el-empty description="暂无订单记录"></el-empty>
    </div>
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router';

const router = useRouter();

const props = defineProps({
  orderRecords: {
    type: Array,
    default: () => []
  }
});

const emit = defineEmits(['view-order', 'pay-order']);

// 方法：获取订单状态类型
const getOrderStatusType = (status) => {
  const statusMap = {
    'pending': 'warning',
    'completed': 'success',
    'cancelled': 'info',
    'refunded': 'danger'
  };
  return statusMap[status] || 'info';
};

// 方法：获取订单状态文本
const getOrderStatusText = (status) => {
  const statusMap = {
    'pending': '待支付',
    'completed': '已完成',
    'cancelled': '已取消',
    'refunded': '已退款'
  };
  return statusMap[status] || status;
};

// 方法：查看订单
const viewOrder = (order) => {
  emit('view-order', order);
};

// 方法：支付订单
const payOrder = (order) => {
  emit('pay-order', order);
};
</script>

<style scoped>
/* 订单表格样式 */
.order-course {
  display: flex;
  align-items: center;
}

.order-course .course-image {
  width: 60px;
  height: 40px;
  margin-right: 10px;
  object-fit: cover;
  border-radius: 4px;
}

.order-course .course-name {
  font-size: 14px;
}

/* 空数据样式 */
.empty-data {
  padding: 30px 0;
}
</style>